package com.sk.bilibili.service;

import com.sk.bilibili.other.R;
import com.sk.bilibili.property.MinioProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 邵康
 * @date 2021/12/4 9:04
 */
@Service
public class AndroidService {

    @Autowired
    private MinioService minioService;
    @Autowired
    private MinioProperty minioProperty;
    private static final String FLASH_PATH="phone/flash.jpg";
    private static final String APP_PATH="phone/app.apk";
    private static final String APP_PAGE_PATH="phone/app.html";
    private static final String URL_PATTERN="%s%s?v=%d";
    private int flashVersion=0;
    private int appVersion=0;

    public R<Void> updateFlash(MultipartFile flash) {
        flashVersion++;
        minioService.uploadStream(flash,FLASH_PATH);
        return R.success();
    }

    public R<Void> updateApp(MultipartFile app, Integer appVersion) {
        this.appVersion=appVersion;
        minioService.uploadStream(app,APP_PATH);
        return R.success();
    }

    public R<String> getFlash() {
        return R.success(String.format(URL_PATTERN, minioProperty.getBasePath(),FLASH_PATH,flashVersion));
    }

    public R<String> getApp() {//获取APP更新信息时获取的是更新页的信息
        return R.success(String.format(URL_PATTERN, minioProperty.getBasePath(),APP_PAGE_PATH,appVersion));
    }

    public R<Void> updateAppPage(MultipartFile appPage) {
        minioService.uploadStream(appPage,APP_PAGE_PATH);
        return R.success();
    }

}
